//LCR 049. 求根节点到叶节点数字之和

class Solution {
public:
    int sumNumbers(TreeNode* root) {
        return dfs(root, 0);
    }
    int dfs(TreeNode* root, int presum)
    {
        int sum = presum * 10 + root->val;
        if(root->left == nullptr && root->right == nullptr) return sum;
        int ret = 0;
        if(root->left) ret += dfs(root->left, sum);
        if(root->right) ret += dfs(root->right, sum);
        return ret;
    }
};